//
//  StudyCardViewController.m
//  xpkc
//
//  Created by 胡志飞 on 15/12/2.
//  Copyright © 2015年 TopSage. All rights reserved.
//

#import "StudyCardViewController.h"
#import "StudyCardTopupSuccViewController.h"

#import "UserMessage.h"
#import "MBProgressHUD.h"
@interface StudyCardViewController ()<MBProgressHUDDelegate>
{
    
    UITextField * _cardidField;
    UITextField * _passwdField;
    
    
    UITextField * _actioncardidField;
    UITextField * _actionpasswdField;
    UITextField * _actionTelField;
    
    UILabel * _noteLabel;
    UILabel * _actionNoteLabel;
}
@end

@implementation StudyCardViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self creatTitleView];
        // Custom initialization
        [self createStudyCardView];
        [self createStudyCardTopupView];
        self.navigationItem.title = @"我的学习卡";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (IsIOS7) {
        [self setEdgesForExtendedLayout:UIRectEdgeNone];
    }
    
    
    
}


-(void)setSelectIndex:(NSInteger)selectIndex{
    
    NSLog(@"%ld--%ld",self.selectIndex,_selectIndex);
    self.segmentController.selectedSegmentIndex = selectIndex;
    
    if (selectIndex == 1) {
        self.studyCardView.hidden = YES;
        self.studyCardTopupView.hidden = NO;
    }
   
    
}

//-(void)viewWillAppear:(BOOL)animated{
//    
//    [super viewWillAppear:animated];
//    
//    NSLog(@"%ld",self.selectIndex);
//    
//    self.segmentController.selectedSegmentIndex = self.selectIndex;
//    
//}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

-(void)createStudyCardTopupView{
    
    //    self.studyCardTopupView.hidden = YES;
    //    self.studyCardView.hidden = YES;
    
    CGFloat offwidth = 0.04 * SCREEN_WIDTH;
    
    CGFloat textFieldH = 35;
    
    UILabel * cardIdLabel = [[UILabel alloc] initWithFrame:CGRectMake(offwidth * 2, 20, 60, textFieldH)];
    cardIdLabel.text = @"卡号";
    cardIdLabel.font = [UIFont boldSystemFontOfSize:18];
    [self.studyCardView addSubview:cardIdLabel];
    
    UITextField * cardIdTextfiled = [[UITextField alloc] initWithFrame:CGRectMake(cardIdLabel.frame.origin.x + 70, 20, SCREEN_WIDTH - cardIdLabel.frame.origin.x - offwidth * 2 - 70, textFieldH)];
    cardIdTextfiled.borderStyle = UITextBorderStyleBezel;
    cardIdTextfiled.backgroundColor = BGColor;
    _cardidField = cardIdTextfiled;
    [self.studyCardView addSubview:cardIdTextfiled];
    
    UILabel * noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(cardIdTextfiled.frame.origin.x , textFieldH + 20 , SCREEN_WIDTH - cardIdTextfiled.frame.origin.x - offwidth * 2, 20)];
    noteLabel.text = @"注意：字母区分大小写";
    noteLabel.font = [UIFont systemFontOfSize:14];
    [self.studyCardView addSubview:noteLabel];
    
    
    UILabel * pwdLabel = [[UILabel alloc] initWithFrame:CGRectMake(offwidth * 2, noteLabel.frame.origin.y + 20 + 10, 60, textFieldH)];
    pwdLabel.text = @"密码";
    pwdLabel.font = [UIFont boldSystemFontOfSize:18];
    
    [self.studyCardView addSubview:pwdLabel];
    
    UITextField * pwdTextfiled = [[UITextField alloc] initWithFrame:CGRectMake(pwdLabel.frame.origin.x + 70, noteLabel.frame.origin.y + 20 + 10, SCREEN_WIDTH - pwdLabel.frame.origin.x - offwidth * 2 - 70, textFieldH)];
    pwdTextfiled.borderStyle = UITextBorderStyleBezel;
    pwdTextfiled.backgroundColor = BGColor;
    pwdTextfiled.secureTextEntry=YES;
    _passwdField = pwdTextfiled;
    [self.studyCardView addSubview:pwdTextfiled];
    
    
    UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(offwidth * 8  , pwdLabel.frame.origin.y + textFieldH + offwidth * 2 , SCREEN_WIDTH - offwidth * 16 , offwidth * 3)];
    
    [btn setTitle:@"立即充值" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor orangeColor]];
    [btn addTarget:self action:@selector(topUp:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = 33;
    [self.studyCardView addSubview:btn];
    
    UILabel * topUpLabel = [[UILabel alloc] initWithFrame:CGRectMake(offwidth * 2, btn.frame.origin.y + offwidth * 3 , SCREEN_WIDTH - offwidth * 4, 40)];
    topUpLabel.text = @"卡号或者密码输入错误，请重新输入或者联系客服人员";
    topUpLabel.font = [UIFont systemFontOfSize:14];
    topUpLabel.numberOfLines = 0;
    _noteLabel = topUpLabel;
    topUpLabel.hidden = YES;
    [self.studyCardView addSubview:topUpLabel];
    
}


-(void)createStudyCardView{
    
    
    self.studyCardView.hidden = NO;
    self.studyCardTopupView.hidden = YES;
    
    
    CGFloat offwidth = 0.04 * SCREEN_WIDTH;
    
    CGFloat textFieldH = 40;
    
    
    UILabel * cardIdLabel = [[UILabel alloc] initWithFrame:CGRectMake(offwidth * 2, 20, 60, textFieldH)];
    cardIdLabel.text = @"卡号";
    cardIdLabel.font = [UIFont boldSystemFontOfSize:18];
    [self.studyCardTopupView addSubview:cardIdLabel];
    
    UITextField * cardIdTextfiled = [[UITextField alloc] initWithFrame:CGRectMake(cardIdLabel.frame.origin.x + 70, 20, SCREEN_WIDTH - cardIdLabel.frame.origin.x - offwidth * 2 - 70, textFieldH)];
    cardIdTextfiled.borderStyle = UITextBorderStyleBezel;
    cardIdTextfiled.backgroundColor = BGColor;
    _actioncardidField = cardIdTextfiled;
    [self.studyCardTopupView addSubview:cardIdTextfiled];
    
    UILabel * noteLabel = [[UILabel alloc] initWithFrame:CGRectMake(cardIdTextfiled.frame.origin.x , textFieldH + 20 , SCREEN_WIDTH - cardIdTextfiled.frame.origin.x - offwidth * 2, 20)];
    noteLabel.text = @"注意：字母区分大小写";
    noteLabel.font = [UIFont systemFontOfSize:14];
    [self.studyCardTopupView addSubview:noteLabel];
    
    
    UILabel * pwdLabel = [[UILabel alloc] initWithFrame:CGRectMake(offwidth * 2, noteLabel.frame.origin.y + 20 + 10, 60, textFieldH)];
    pwdLabel.text = @"密码";
    pwdLabel.font = [UIFont boldSystemFontOfSize:18];
    [self.studyCardTopupView addSubview:pwdLabel];
    
    UITextField * pwdTextfiled = [[UITextField alloc] initWithFrame:CGRectMake(pwdLabel.frame.origin.x + 70, noteLabel.frame.origin.y + 20 + 10, SCREEN_WIDTH - pwdLabel.frame.origin.x - offwidth * 2 - 70, textFieldH)];
    pwdTextfiled.borderStyle = UITextBorderStyleBezel;
    pwdTextfiled.backgroundColor = BGColor;
    _actionpasswdField = pwdTextfiled;
    [self.studyCardTopupView addSubview:pwdTextfiled];
    
    UILabel * phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(offwidth * 2 , pwdLabel.frame.origin.y + textFieldH + 30, 60, textFieldH)];
    phoneLabel.text = @"手机号";
    phoneLabel.font = [UIFont boldSystemFontOfSize:18];
    [self.studyCardTopupView addSubview:phoneLabel];
    
    UITextField * phoneTextfiled = [[UITextField alloc] initWithFrame:CGRectMake(pwdTextfiled.frame.origin.x, pwdTextfiled.frame.origin.y + textFieldH + 30, SCREEN_WIDTH - pwdLabel.frame.origin.x - offwidth * 2 - 70, textFieldH)];
    phoneTextfiled.borderStyle = UITextBorderStyleBezel;
    _actionTelField = phoneTextfiled;
    [self.studyCardTopupView addSubview:phoneTextfiled];
    phoneTextfiled.backgroundColor = BGColor;
    
    UILabel * phoneNotelabel = [[UILabel alloc] initWithFrame:CGRectMake(phoneTextfiled.frame.origin.x , phoneTextfiled.frame.origin.y + textFieldH , SCREEN_WIDTH - pwdLabel.frame.origin.x - offwidth * 2 - 70, 40)];
    
    phoneNotelabel.text = @"注意：填写正确的手机号码，方便提供考前预测等重要信息！";
    phoneNotelabel.numberOfLines = 0;
    phoneNotelabel.font = [UIFont systemFontOfSize:14];
    [self.studyCardTopupView addSubview:phoneNotelabel];
    
    
    
    UIButton * btn = [[UIButton alloc] initWithFrame:CGRectMake(offwidth * 8  , phoneNotelabel.frame.origin.y + 20 + offwidth * 2, SCREEN_WIDTH - offwidth * 16 , textFieldH)];
    
    [btn setTitle:@"立即充值" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor orangeColor]];
    [btn addTarget:self action:@selector(topUp:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = 34;
    [self.studyCardTopupView addSubview:btn];
    
    UILabel * topUpLabel = [[UILabel alloc] initWithFrame:CGRectMake(offwidth * 2, btn.frame.origin.y + textFieldH + 5, SCREEN_WIDTH - offwidth * 4, 40)];
    topUpLabel.text = @"卡号或者密码输入错误，请重新输入或者联系客服人员";
    topUpLabel.font = [UIFont systemFontOfSize:14];
    topUpLabel.numberOfLines = 0;
    topUpLabel.hidden = YES;
    _actionNoteLabel = topUpLabel;
    [self.studyCardTopupView addSubview:topUpLabel];
    
    
    
    
}

-(void)creatTitleView{
    
    self.view.backgroundColor = [UIColor whiteColor];
    NSArray *array = @[@"学习卡",@"活动学习卡"];
    //创建控件
    
    self.segmentController = [[UISegmentedControl alloc]initWithItems:array];
    [self.segmentController addTarget:self action:@selector(segmentChanged) forControlEvents:UIControlEventValueChanged];
    //设定位置
    CGRect segmengt_frame;
    CGFloat line_y;
    CGRect studyCardView_frame;
    if (isPad) {
        //适配
        segmengt_frame = RECT(SCREEN_WIDTH/5, 10, SCREEN_WIDTH*3/5, 40);
        line_y = 60;
        studyCardView_frame = RECT(0, 60, SCREEN_WIDTH, SCREEN_HEIGHT-60-64);
    }else{
        segmengt_frame = RECT(SCREEN_WIDTH/5, 10, SCREEN_WIDTH*3/5, 30);
        line_y = 50;
        studyCardView_frame = RECT(0, 60, SCREEN_WIDTH, SCREEN_HEIGHT-60-64);
    }
    
    self.studyCardView = [[UIImageView alloc] initWithFrame:studyCardView_frame];
    self.studyCardView.userInteractionEnabled = YES;
    [self.view addSubview:self.studyCardView];
    
    
    self.studyCardTopupView = [[UIImageView alloc] initWithFrame:studyCardView_frame];
    self.studyCardTopupView.userInteractionEnabled = YES;
    [self.view addSubview:self.studyCardTopupView];
    
    self.segmentController.frame = segmengt_frame;
    
    //详细信息
    self.segmentController.tintColor = [UIColor orangeColor];
    ;
    self.segmentController.backgroundColor = [UIColor whiteColor];
    self.segmentController.selectedSegmentIndex = 0;
    //添加视图
    
    [self.view addSubview:self.segmentController];
    //视图上方黄色线
    
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(1, line_y, SCREEN_WIDTH - 2, 2)];
    label.backgroundColor=OrangeColor;
    [self.view addSubview:label];
    
}

/**
 *  segment点击
 */
-(void)segmentChanged{
    
    if (self.segmentController.selectedSegmentIndex == 0) {
        
        
        
        self.studyCardTopupView.hidden = YES;
        
        self.studyCardView.hidden = NO;
        
        
    }else{
        
        
        
        self.studyCardView.hidden = YES;
        self.studyCardTopupView.hidden = NO;
        
        
    }
}


// 充值
-(void)topUp:(UIButton *)btn{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.delegate = self;
    
    NSString *module = @"learncard";
    NSString *interface = @"rechargecard";
    
    NSString *timeString = [GetTime getTime];
    NSString *hashString = [[NSString stringWithFormat:@"%@/%@/key/%@/time/%@",module,interface,KEY,timeString] md5];
    NSString *urlString = [NSString stringWithFormat:@"%@/laravel/public/%@/%@/hash/%@/appid/1",URL2,module,interface,hashString];
    NSDictionary *parameter;
    if (btn.tag == 33) {
        // 学习卡充值
        parameter = @{@"time":timeString,@"uid":[UserMessage sharedUserMessage].userMid,@"type":@"card",@"cardid":_cardidField.text,@"passwd":_passwdField.text};
    }else if (btn.tag == 34){
        // 学习卡活动充值
        parameter = @{@"time":timeString,@"uid":[UserMessage sharedUserMessage].userMid,@"type":@"zcard",@"cardid":_actioncardidField.text,@"passwd":_actionpasswdField.text,@"tel":_actionTelField.text};
    }
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
//    NSLog(@"urlstring--%@",urlString);
//    NSLog(@"parameter--%@",parameter);
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:urlString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        logdebug(@"%@",dic);
        
        
        if([[dic objectForKey:@"succ"] intValue]){
            
            [self performSelectorOnMainThread:@selector(succTopup:) withObject:@"yes" waitUntilDone:YES];
            
        }else{
            [self performSelectorOnMainThread:@selector(succTopup:) withObject:@"no" waitUntilDone:YES];
        }
        
        [hud hide:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      
        [hud hide:YES];
        NSLog(@"shi bai ");
    }];
    
    
    
    
    
    
    
    
    
    
}


-(void)succTopup:(NSString *)str{
    if ([str isEqualToString:@"yes"]) {
        _noteLabel.hidden = YES;
        _actionNoteLabel.hidden = YES;
        StudyCardTopupSuccViewController * studySucc = [[StudyCardTopupSuccViewController alloc] init];
        [self.navigationController pushViewController:studySucc animated:YES];

    }else{
        
        if (self.segmentController.selectedSegmentIndex == 0) {
            _noteLabel.hidden = NO;
        }else if(self.segmentController.selectedSegmentIndex == 1){
            _actionNoteLabel.hidden = NO;
            
        }
        
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
